﻿using Autofac;
using HSharp.Autofac.Attributes;
using HSharp.Business;
using Microsoft.AspNetCore.Mvc;

namespace WebApi7A
{
    public class AutofacRegisterModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            //获取所有控制器类型并使用属性注入
            Type[] controllersTypeAssembly = typeof(Program).Assembly.GetExportedTypes()
                .Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToArray();
            builder.RegisterTypes(controllersTypeAssembly).PropertiesAutowired(new AutowiredPropertySelector());
            //批量自动注入,把需要注入层的程序集传参数,注入Service层的类

            //builder.BatchAutowired(typeof(UserService).Assembly);

            builder.RegisterType<UserService>().As<IUserService>().InstancePerDependency();

            //注入其它层的containerBuilder.BatchAutowired(typeof(其它层的任务一个类).Assembly);
        }
    }
}